package main;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Vector;
import streamdef.StreamDefinitionManager;
import streamdef.StreamDefinitionParser;
import streamproc.Chain;
import database.EventSerializer;
import event.*;

public class CEPLFunctions {
	public static class Main {
		public static synchronized void init(boolean usePostgre,
				boolean useMySQL, String dbURL, String dbUser, String dbPassword) {
			EventSerializer.instance(usePostgre, useMySQL, dbURL, dbUser,
					dbPassword);
			EventCallbackManager.instance();
			EventFormatManager.instance();
			StreamDefinitionManager.instance();
			StreamDefinitionParser.instance();
			Chain.instance();

			EventCallbackManager.instance().init();
			EventFormatManager.instance().init();
			StreamDefinitionManager.instance().init();
			StreamDefinitionParser.instance().init();
			Chain.instance().reset();
		}

		public static void saveConfiguration(String fileName) {
			// saving queries
		}

		public static void loadConfiguration(String fileName) {
			// load queries
		}

		public static synchronized void reset() {
			EventCallbackManager.instance().init();
			EventFormatManager.instance().init();
			StreamDefinitionManager.instance().init();
			StreamDefinitionParser.instance().init();
			Chain.instance().reset();
		}

		// After all the callbacks and queries are set
		// -> create the query plan so you can start pushing events
		public static synchronized void start() {
			Chain.instance().init();
		}
	}

	public static class EventCallbacks {
		public static synchronized void addEventCallback(long streamID,
				EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().addEventCallback(streamID, object);
		}

		public static synchronized void activateEventCallback(long streamID,
				EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().activateEventCallback(streamID,
					object);
		}

		public static synchronized void deactivateEventCallback(long streamID,
				EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().deactivateEventCallback(streamID,
					object);
		}

		public static synchronized void removeEventCallback(long streamID,
				EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().removeEventCallback(streamID,
					object);
		}

		// same functions as above, but for callback-functions that will be
		// called for a set of new complex events, caused by the same
		// pushed event
		public static synchronized void addEventVectorCallback(long streamID,
				EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().addEventVectorCallback(streamID,
					object);
		}

		public static synchronized void activateEventVectorCallback(
				long streamID, EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().activateEventVectorCallback(
					streamID, object);
		}

		public static synchronized void deactivateEventVectorCallback(
				long streamID, EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().deactivateEventVectorCallback(
					streamID, object);
		}

		public static synchronized void removeEventVectorCallback(
				long streamID, EventCallbackFunctionOwner object) {
			EventCallbackManager.instance().removeEventVectorCallback(streamID,
					object);
		}

		// for testing purposes only
		public static void fireEvent(Event e) {
			EventCallbackManager.instance().fireEvent(e);
		}
	}

	public static class Streams {
		public static synchronized void addStreamDefinition(String querystring)
				throws ParseException {
			StreamDefinitionParser.instance().parse(querystring);
		}

		public static synchronized void activateStream(long streamID) {
		}

		public static synchronized void deactivateStream(long streamID) {
		}

		public static synchronized void removeStream(long streamID) {
		}
	}

	public static class Events {
		// the user of this library is responsible for keeping track of the
		// generators and their events
		// this library abstracts generators to streamIDs
		@Deprecated
		public static void addEventFormat(long streamID,
				Vector<DEFINES.CEPLEventDataFieldDefinition> format) {
			EventFormatManager.instance().addEventFormat(streamID, format);
		}

		public static synchronized void pushEvent(long streamID, byte[] data) {
			Chain.instance().processByteArray(streamID, data);
		}

		public static synchronized void pushEvent(long streamID, byte[] data,
				java.sql.Timestamp timestamp) {
			Chain.instance().processByteArrayWithTimeStamp(streamID, data,
					timestamp);
		}

		// for testing purposes only
		public static DEFINES.CEPLEventDataFieldType getFieldValueType(
				long streamID, String fieldName) {
			return EventFormatManager.instance().getFieldValueType(streamID,
					fieldName);
		}

		public static HashMap<String, DEFINES.CEPLEventDataFieldValue> parseEvent(
				long streamID, byte[] data) {
			return EventFormatManager.instance().parseByteArray(streamID, data);
		}
	}

}